define(function(require){
    $(function(){
    	/**
         * @name Init
         * @class 顶部初始化 。
         */
    	/*---------- 顶部初始化 ----------*/
    	require('validate');
    	$('.btn-user').on('click', function(){
    		$dialog({
    			title: '个人信息',
    			width: '500px',
    			onshow: function(){
    				var $d = this, $node = $(this.node), defhtml = '<span class="gray">—</span>';
    				$ajax.ajax({
    					url: '/trp/user/get_user/',
    					data:{
    						userId: userId
    					},
    					success: function(data){
    						$d.content(['<div class="pop-form form-group">',
	    						        	'<form class="editInfoForm">',
		    						    		'<div class="form_item">',
		    						    			'<label class="form_label">用户名：</label>',
		    						    			'<div class="form_ctrl user_name"></div>',
		    						    		'</div>',
		    						    		'<div class="form_item">',
		    						    			'<label class="form_label">手机号码：</label>',
		    						    			'<div class="form_ctrl">',
		    						    				'<span class="isdata user_phone"></span>',
		    						    				'<input type="text" name="user_phone" class="form-control phone" placeholder="请填写手机号码..." style="display:none;"/>',
		    						    			'</div>',
		    						    		'</div>',
		    						    		'<div class="form_item">',
		    						    			'<label class="form_label">邮箱：</label>',
		    						    			'<div class="form_ctrl">',
		    						    				'<span class="isdata user_email"></span>',
		    						    				'<input type="text" name="user_email" class="form-control email" placeholder="请填写邮箱..." style="display:none;"/>',
		    						    			'</div>',
		    						    		'</div>',
		    						    	'</form>',
		    						    	'<form class="resetForm" style="display:none;">',
			    						    	'<div class="form_item">',
					    							'<label class="form_label">原密码：</label>',
					    							'<div class="form_ctrl">',
					    								'<input type="password" name="password_old" class="form-control required password" placeholder="请填写原密码..."/>',
					    							'</div>',
					    						'</div>',
					    						'<div class="form_item">',
					    							'<label class="form_label">新密码：</label>',
					    							'<div class="form_ctrl">',
					    								'<input type="password" name="password" class="form-control required password" placeholder="请填写新密码..."/>',
					    							'</div>',
					    						'</div>',
					    						'<div class="form_item">',
					    							'<label class="form_label">确认新密码：</label>',
					    							'<div class="form_ctrl">',
					    								'<input type="password" name="password_confirm" class="form-control required {equalTo:\'.usernpwd\'}" placeholder="请确认新密码..."/>',
					    							'</div>',
					    						'</div>',
					    					'</form>',
		    						    '</div>'].join(''));
    						
    						$('.user_name',$node).html(semi.utils.isEmpty(data.username)?defhtml:data.username);
    						$('.user_phone',$node).html(semi.utils.isEmpty(data.phone)?defhtml:data.phone).siblings('[name=user_phone]').val(data.phone);
    						$('.user_email',$node).html(semi.utils.isEmpty(data.email)?defhtml:data.email).siblings('[name=user_email]').val(data.email);
    					},
    				});
    			},
        	    button: [
                     {
                         value: '修改信息',
                         callback: function(){
                        	var $d = this, $node = $($d.node);
                        	$('span.isdata',$node).hide().siblings('.form-control').show();
                        	$d.title('修改个人信息');
                        	$d.button([
                        	    {
                        	    	value: '保存',
                        	    	callback: function(){
                        	    		if(!$('.editInfoForm',$node).valid())return false;
                                 		$ajax.ajax({
                                 			url : '/trp/user/do_modify/',
                                     		data: JSON.stringify({
                                     			name:$('.user_name',$node).html(),
                                         		phone: $('.phone',$node).val(),
                                         		email: $('.email',$node).val()
                             				}),
                                     		contentType : "application/json",
                                             type : 'POST',
                                             dataType : 'json',
                                             success: function(){
                                             	$d.close().remove();
                                             	$dialog.alert('修改信息成功！','success');
                                             },
                                             error: function(){
                                             	$d.close().remove();
                                             	$dialog.alert('修改信息失败，请稍后重试！','warning');
                                             }
                                     	 });
                        	    	},
                        	    	autofocus: true
                        	    },
                        	    {
                        	    	value: '取消'
                        	    }
                        	]);
                         	return false;
                         }
                     },
                     {
                    	 value: '重置密码',
                    	 callback: function(){
                    		var $d = this, $node = $($d.node);
                    		$('.editInfoForm',$node).hide();
             	    		$('.resetForm',$node).show();
             	    		$('[name=password]',$node).addClass('usernpwd');
                         	$d.title('重置密码');
                         	$d.button([
                         	    {
                         	    	value: '保存',
                         	    	callback: function(){
                         	    		if(!$('.resetForm',$node).valid())return false;
                                 		$ajax.ajax({
                                 			url : '/trp/user/reset_password/',
                                     		data: JSON.stringify({
                                     			user_id: userId,
                                     			password_old: $.trim($('[name=password_old]',$node).val()),
                                     			password: $.trim($('[name=password]',$node).val()),
                    	            			password_confirm: $.trim($('[name=password_confirm]',$node).val())
                             				}),
                                     		contentType : "application/json",
                                             type : 'POST',
                                             dataType : 'json',
                                             success: function(){
                                             	$d.close().remove();
                                             	$dialog.alert('修改信息成功！','success');
                                             },
                                             error: function(data){
                                             	$dialog.alert(data.message,'warning');
                                             }
                                     	 });
                                 		return false;
                         	    	},
                         	    	autofocus: true
                         	    },
                         	    {
                         	    	value: '取消'
                         	    }
                         	]);
                          	return false;
                    	 }
                     },
                     {
	             		 value: '关闭'
	             	 }
                 ]
            }).showModal();
    	});
    	
    	/* 搜索框鼠标经过选中 */
    	$(document).on('mouseover', '.searchbox-ipt', function(){
    		$(this).select()
    	});
    });
});